package com.jackchong.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.jackchong.base.BaseApplication;
import com.jackchong.common.PhotoActivity;
import com.utils.R;

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/11/20 14:18
 */

@SuppressLint("AppCompatCustomView")
public class JImageView extends ImageView{

    public static final String TRANSITION_NAME = "shareElement";

    private int mJPadding;
    private int mClickGap;
    private long mOldClickTime;
    private boolean mJPreView;

    private String mImageUrl;
    private int mImageResource;

    public JImageView(Context context) {
        super(context);
        init();
    }

    public JImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public JImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.JImageView);
        parseAttr(typedArray);
        typedArray.recycle();
    }

    public JImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void parseAttr(TypedArray typedArray){
        Drawable drawable = typedArray.getDrawable(R.styleable.JImageView_JSrc);
        mJPreView = typedArray.getBoolean(R.styleable.JImageView_JPreView, false);
        if (drawable != null && !BaseApplication.RELEASE_MODE){
            setImageDrawable(drawable);
        }
        boolean select = typedArray.getBoolean(R.styleable.JImageView_JSelect, false);
        if (select){
            setSelected(select);
        }
        mJPadding = (int) typedArray.getDimension(R.styleable.JImageView_JPadding, 0);
        mClickGap = typedArray.getInteger(R.styleable.JImageView_JClickGap, 0);
        if (mJPreView){
            setOnClickListener(v -> {
                if (!TextUtils.isEmpty(mImageUrl)){
                    if (getContext() instanceof Activity){
                        PhotoActivity.start((Activity) getContext(), mImageUrl, v);
                    }else {
                        PhotoActivity.start(getContext(), mImageUrl);
                    }
                }else if (mImageResource != 0){
                    if (getContext() instanceof Activity){
                        PhotoActivity.start((Activity) getContext(), mImageResource, v);
                    }else {
                        PhotoActivity.start(getContext(), mImageResource);
                    }
                }
            });
        }
    }

    @Override
    public void setLayoutParams(ViewGroup.LayoutParams params) {

        if (getPaddingLeft() == 0 && getPaddingTop() == 0 && getPaddingRight() == 0 && getPaddingBottom() == 0 && mJPadding != 0) {
            setPadding(mJPadding, mJPadding, mJPadding, mJPadding);
            params.width += mJPadding * 2;
            params.height += mJPadding * 2;
        }
        super.setLayoutParams(params);

    }

    @Override
    public void setPressed(boolean pressed) {
        super.setPressed(pressed);
        float alpha = pressed && isClickable() ? 0.8f : 1f;
        if (alpha != getAlpha()){
            setAlpha(alpha);
        }
    }

    public void gone(boolean gone){
        setVisibility(gone ? GONE : VISIBLE);
    }

    public void invisible(boolean invisible){
        setVisibility(invisible ? INVISIBLE : VISIBLE);
    }


    public void gone(){
        setVisibility(GONE);
    }

    public void invisible(){
        setVisibility(INVISIBLE);
    }

    public void visible(){
        setVisibility(VISIBLE);
    }

    public void visible(boolean visible){
        setVisibility(visible? VISIBLE : GONE);
    }


    private void init() {

    }

    @Override
    public boolean performClick() {
        long millis = System.currentTimeMillis();
        if (millis - mOldClickTime < mClickGap){
            return false;
        }
        mOldClickTime = millis;
        return super.performClick();
    }


    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
        mImageResource = resId;
    }

    public void loadImageUrl(int resId) {
        mImageResource = resId;
        if (getContext() instanceof Activity && ((Activity) getContext()).isDestroyed()) {
            return;
        }
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.icon_fail)
                .error(R.drawable.icon_fail);
        Glide.with(this)
                .load(resId)
                .apply(options)
                .into(this);
    }

    public void loadImageUrl(int resId, int placeholderId, int errorId) {
        mImageResource = resId;
        if (getContext() instanceof Activity && ((Activity) getContext()).isDestroyed()) {
            return;
        }
        RequestOptions options = new RequestOptions()
                .placeholder(placeholderId)
                .error(errorId);
        Glide.with(this)
                .load(resId)
                .apply(options)
                .into(this);
    }


    public void loadImageUrl(String url) {
        mImageUrl = url;
        if (getContext() instanceof Activity && ((Activity) getContext()).isDestroyed()) {
            return;
        }
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.icon_fail)
                .error(R.drawable.icon_fail);
        Glide.with(this)
                .load(url)
                .apply(options)
                .into(this);
    }

    public void loadAvatar(String url) {
        mImageUrl = url;
        if (!((Activity) getContext()).isDestroyed()) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.default_avatar)
                    .error(R.drawable.default_avatar);
            Glide.with(this)
                    .load(url)
                    .apply(options)
                    .into(this);
        }
    }


    public String getImageUrl(){
        return mImageUrl;
    }

    public void setGif(@DrawableRes int gif){
        Glide.with(getContext())
                .load(gif)
                .into(this);
    }

    public void loadImageUrl(String url, @DrawableRes int perch) {
        mImageUrl = url;
            if (!((Activity) getContext()).isDestroyed()) {
                RequestOptions options = new RequestOptions()
                        .placeholder(perch)
                        .error(perch);
                Glide.with(getContext())
                        .load(url)
                        .apply(options)
                        .into(this);
            }

    }
}
